package com.bnyy.medicalHousekeeper.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.OrderAdapter;
import com.bnyy.medicalHousekeeper.base.RefreshFragment;
import com.bnyy.medicalHousekeeper.bean.Order;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/9
 **/
public class OrderListFragment extends RefreshFragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int userId;
    private int type;
    private int page = 1;
    private int size = 20;
    private OrderAdapter adapter;

    public static OrderListFragment getInstance(int userId, int type) {
        OrderListFragment fragment = new OrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("userId", userId);
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getOrderList();
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getOrderList();
            }
        };
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        userId = getArguments().getInt("userId", -1);
        type = getArguments().getInt("type", 1);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OrderAdapter(mContext);
        recyclerView.setAdapter(adapter);

        mRefreshLayout.autoRefresh();
    }

    private void getOrderList() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page_index", page);
        params.put("page_size", size);
        params.put("id", userId);
        params.put("type", type);

        requestManager.request(requestManager.mJavaRetrofitService.getOrderList(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<Order>>(mRefreshLayout) {
                    @Override
                    public void onSuccess(ArrayList<Order> orders) {
                        super.onSuccess(orders);

                        mRefreshLayout.setEnableLoadMore(orders.size() >= size);

                        if (page == 1) {
                            adapter.refresh(orders);
                            if (orders.size() > 0) {
                                llNoData.setVisibility(View.GONE);
                            } else {
                                llNoData.setVisibility(View.VISIBLE);
                            }
                        } else {
                            adapter.loadMore(orders);
                        }
                    }
                });
    }
}















































